﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameFramework.GameLogic;
using Microsoft.Xna.Framework;
using GameFramework.Controller;
using Microsoft.Xna.Framework.Input;
using GameFramework.Camera;

namespace MyRaceGame.GameLogic.GameStates
{
    public class DebugState : RaceGameState
    {
        public DebugState(RaceGame raceGame)
            : base(raceGame)
        {
            raceGame.Reset();
            RaceController.FreeRace = true;
            RaceController.RemoveAllExceptHumanRacer();
        }

        protected override void UpdateKeyboard(GameTime gameTime)
        {
            if (InputController.KeyPressed(Keys.X))
            {
                raceGame.Reset();
                GameState = new BeginningState(raceGame);
            }

            else if (InputController.KeyPressed(Keys.C))
                raceGame.ChangeCameraMode(CameraMode.ChaseCamera);

            else if (InputController.KeyPressed(Keys.F))
                raceGame.ChangeCameraMode(CameraMode.FreeCamera);

            else if (InputController.KeyPressed(Keys.A))
                changeProperty = ChangeProperty.AccFactor;

            else if (InputController.KeyPressed(Keys.D))
                changeProperty = ChangeProperty.DecFactor;

            else if (InputController.KeyPressed(Keys.M))
                changeProperty = ChangeProperty.MaxSpeed;
            
            else if (InputController.IsKeyDown(Keys.Up))
            {
                switch (changeProperty)
                {
                    case ChangeProperty.AccFactor:
                        InputController.AccFactor += .1f;
                        break;
                    case ChangeProperty.DecFactor:
                        InputController.DecFactor += .1f;
                        break;
                    case ChangeProperty.MaxSpeed:
                        InputController.MaxSpeed += 100f;
                        break;
                }
            }

            else if (InputController.IsKeyDown(Keys.Down))
            {
                switch (changeProperty)
                {
                    case ChangeProperty.AccFactor:
                        InputController.AccFactor -= .1f;
                        break;
                    case ChangeProperty.DecFactor:
                        InputController.DecFactor -= .1f;
                        break;
                    case ChangeProperty.MaxSpeed:
                        InputController.MaxSpeed -= 100f;
                        break;
                }
            }
        }

        protected override void UpdateRacers(GameTime gameTime)
        {
            RaceController.HumanRacer.PlaySpeed = InputController.Speed * 0.1f;
            raceGame.UpdateRacers(gameTime);
        }

        protected override void DrawRacers(GameTime gameTime)
        {
            raceGame.DrawModels();
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.DrawRaceTrack();
        }

        protected override void DrawText(GameTime gameTime)
        {
            TextDrawController.Begin(TextAlignment.Left);

            TextDrawController.WriteLine("Debug Mode");
            TextDrawController.WriteLine("Press X to restart game");
            TextDrawController.WriteLine("Lap = {0}/{1}", RaceController.CurrentLap, RaceController.Laps);
            TextDrawController.WriteLine("Speed = {0}", RaceController.HumanRacer.Speed);
            TextDrawController.WriteLine("RaceFinished = {0}", RaceController.RaceFinished);
            TextDrawController.WriteLine();
            TextDrawController.WriteLine("AccFactor = {0} Press A to modify", InputController.AccFactor);
            TextDrawController.WriteLine("DecFactor = {0} Press D to modify", InputController.DecFactor);
            TextDrawController.WriteLine("MaxSpeed = {0} Press M to modify", InputController.MaxSpeed);
            
            TextDrawController.End();

            TextDrawController.Begin(TextAlignment.Right);
            
            TextDrawController.WriteLine("CameraMode = {0}", raceGame.CameraMode);
            TextDrawController.WriteLine("Editable values:");

            switch (raceGame.CameraMode)
            {
                case CameraMode.ChaseCamera:
                    ChaseCamera chaseCamera = raceGame.CurrentCamera as ChaseCamera;
                    TextDrawController.WriteLine("(1) Position = {0}", chaseCamera.Position);
                    TextDrawController.WriteLine("(2) PositionOffset = {0}", chaseCamera.PositionOffset);
                    TextDrawController.WriteLine("(3) TargetOffset = {0}", chaseCamera.TargetOffset);
                    TextDrawController.WriteLine("(4) RelativeCameraRotation = {0}", chaseCamera.RelativeCameraRotation);
                    break;
                case CameraMode.FreeCamera:
                    FreeCamera freeCamera = raceGame.CurrentCamera as FreeCamera;
                    TextDrawController.WriteLine("(1) Position = {0}", freeCamera.Position);
                    TextDrawController.WriteLine("(2) Yaw = {0}", freeCamera.Yaw);
                    TextDrawController.WriteLine("(3) Pitch = {0}", freeCamera.Pitch);
                    break;
            }

            TextDrawController.End();
        }

        enum ChangeProperty
        {
            None = 0,
            AccFactor,
            DecFactor,
            MaxSpeed
        }

        ChangeProperty changeProperty = ChangeProperty.None;
    }
}
